package com.hyy.controller.manage;

import com.hyy.bean.Merchandise;
import com.hyy.service.MerchandiseService;
import com.hyy.service.serviceImpl.MerchandiseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "AddProductServlet",urlPatterns = "/addProduct")
public class AddProductServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding( "utf-8" ); //必须写在第一位，因为采用这种方式去读取数据，否则 数据会出错。
		//设置这样方式去读。这样中文就能够读取出来了，但是需要注意。表单的发送方式必须是method='post'
		resp.setContentType( "text/html;charset=utf-8" );
		//获取请求参数

		MerchandiseService merchandiseService = new MerchandiseServiceImpl();
		String productName = req.getParameter("productname");
		String img = req.getParameter("img");
		Double price = Double.valueOf(req.getParameter("price"));
		Long stock = Long.valueOf(req.getParameter("stock"));
		Long category = Long.valueOf(req.getParameter("category"));

		Merchandise merchandise = new Merchandise(productName,price,stock,img,category,0);
		merchandiseService.addNewProduct(merchandise);
		req.getRequestDispatcher("/merchandiseList").forward(req,resp);
	}
}
